package com.ywm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ywm.entity.Content;
import com.ywm.service.ContentService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author ywm
 */
@RequestMapping("/content")
@RestController
public class ContentController {
    @Autowired
    ContentService contentService;

    @ApiOperation("根据博客id分页查询评论")
    @GetMapping("/{page}/{size}/{blogId}")
    public Map<String,Object>  queryContent(@PathVariable int page,@PathVariable int size,@PathVariable String blogId){
        return contentService.queryContent( page,  size,  blogId);
    }

    @ApiOperation("提交评论")
    @PostMapping("/commitContent")
    public String commitContent(@RequestBody Content content) {
        content.setIsDeleted(0);
        content.setVersion(1);
        boolean save = contentService.save(content);
        if (save) {
            return "评论上传成功";
        } else {
            return "评论上传失败";
        }
    }

    @ApiOperation("修改评论")
    @PostMapping("/updateContent")
    public String updateContent(@RequestBody Content content) {
        QueryWrapper<Content> wrapper = new QueryWrapper<>();
        wrapper.eq("content_id", content.getContentId());
        boolean update = contentService.update(wrapper);
        if (update) {
            return "评论修改成功";
        } else {
            return "评论修改失败";
        }
    }

    @ApiOperation("删除评论")
    @GetMapping("/deleteContent/{contentId}")
    public String deleteContent(@PathVariable String contentId) {
        boolean b = contentService.deleteContentById(contentId);
        if (b) {
            return "评论删除成功";
        } else {
            return "评论修改失败";
        }
    }
}
